package lzupdater.timing;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * 
 * @author Fraskor
 */
public class TimerMode {

	/**
	 * Repeat mode of the timer.
	 * 
	 * @author Administrateur
	 * 
	 */
	private enum TimingMode {
		/**
		 * After firing, the timer is put back into the central with a new
		 * firing time (the previous firing time plus the delay).
		 */
		REPEAT,
		/**
		 * After firing, the timer is put back into the central with a new
		 * firing time (the previous firing time plus the delay).
		 */
		EVERYTIME,
		/**
		 * After firing the timer, it is deleted from the central, thus it is
		 * only called once.
		 */
		ONCE
	}

	private final TimingMode mode;
	private final int totalRepeat;
	private int nbRepeated;

	private final static TimerMode EVERYTIME = new TimerMode(
			TimingMode.EVERYTIME);

	/**
	 * Indicate to the TimerMode that the task has been performed
	 */
	public void setTimedTaskPlayed() {
		nbRepeated++;
	}

	/**
	 * @return true if the task associated with this timerMode should not be
	 *         played (i.e. the number of times requested has been perfomed).
	 */
	public boolean doNotPlayAgain() {
		return (mode != TimingMode.EVERYTIME) && nbRepeated >= totalRepeat;
	}

	/**
	 * Get an "only once played" mode
	 */
	public static TimerMode onlyOnce() {
		return repeat(1);
	}

	/**
	 * Get an "every time played" mode
	 */
	public static TimerMode everyTime() {
		return EVERYTIME;
	}

	/**
	 * Get an "X time played" mode
	 * 
	 * @param numberOfTimes
	 *            number of time to perform the task associated with this
	 *            TimerMode
	 */
	public static TimerMode repeat(int numberOfTimes) {
		return new TimerMode(TimingMode.REPEAT, numberOfTimes);
	}

	public void reset() {
		nbRepeated = 0;
	}

	private TimerMode(TimingMode mode) {
		this(mode, (mode == TimingMode.EVERYTIME) ? -1 : 1);
	}

	private TimerMode(TimingMode mode, int nbRepeat) {
		nbRepeated = 0;
		totalRepeat = nbRepeat;
		this.mode = mode;
	}

	@Override
	public String toString() {
		switch (mode) {
		case EVERYTIME:
			return "TimerMode(EVERYTIME)";
		case ONCE:
		case REPEAT:
			return String.format("TimerMode(%s: %d/%d)", mode, nbRepeated,
					totalRepeat);
		}
		return super.toString();
	}

}
